<?php

require_once './library/Zend/Db/Table/Abstract.php';
require_once './library/Table.php';

class TableDashboard extends Zend_Db_Table_Abstract
{

    protected $_name;
    protected $_primary;
    protected $_db;
    protected $_registery;
    private $_select;

    protected function _setup()
    {
        $this->_name = 'dashboard';
        $this->_primary = 'IdDashboard';
        $this->_registry = Zend_Registry::getInstance();
        $this->_db = $this->_registry->get('adapter');
        parent::_setup();
    }

    public function addDashboardModule($dashboard)
    {
        
    }

    public function getListeDashboardModule()
    {
        $select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
        $select->setIntegrityCheck(false)
                ->joinLeft('utilisateur_dashboard',
                        'dashboard.IdDashboard = utilisateur_dashboard.IdDashboard',
                        array('utilisateur_dashboard.IdUtilisateur', 'NbUtilisateur' => 'COUNT(utilisateur_dashboard.IdDashboard)'))
                ->group('dashboard.IdDashboard');
        $row = $this->fetchAll($select);
        return $row;
    }

    public function getListeDashboardModuleByUser($idUser)
    {
        $select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
        $select->setIntegrityCheck(false)
                ->joinLeft('utilisateur_dashboard',
                        'dashboard.IdDashboard = utilisateur_dashboard.IdDashboard',
                        array('utilisateur_dashboard.IdUtilisateur'))
                ->where('utilisateur_dashboard.IdUtilisateur = ?', $idUser);
        $row = $this->fetchAll($select);
        return $row;
    }

    public function getIdUser($pId)
    {
        $this->_select = $this->_db->select();
        $this->_select->from($this->_name);
        $this->_select->where('IdUtilisateur = ?', $pId);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        if (isset($result))
            return $result;
    }

    public function getLstUser()
    {
        $this->_select = $this->_db->select();
        $this->_select->from($this->_name);
        $this->_select = $this->_db->query($this->_select);
        $result = $this->_select->fetchAll();
        if (isset($result[0]))
            return $result;
        return false;
    }

    public function newUser($pName, $pPwd, $pDroit)
    {
        $val = array('NomUtilisateur' => $pName,
            'MdpUtilisateur' => $pPwd,
            'IdModelevisuel' => '0',
            'IdDroit' => $pDroit);
        $this->_db->insert($this->_name, $val);
        $id = $this->_db->lastInsertId($this->_name);
        return $id;
    }

    public function getNbModule($url)
    {
        $select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
        $select->setIntegrityCheck(false)
                ->where('dashboard.Url = ?', $url);
        $rowset = $this->fetchAll($select);
        $count = 0;
        foreach ($rowset as $row)
        {
            $count++;
        }
        return $count;
    }

    public function ajouterModule($libelle, $url, $desc, $refresh, $img, $auteur, $email)
    {
        $data = array(
            'ModuleDashboard' => $libelle,
            'Description' => $desc,
            'Url' => $url,
            'Img' => $img,
            'Refresh' => $refresh,
	    'Auteur' => $auteur,
	    'Email' => $email
        );
        $this->insert($data);
    }

    public function getNbModuleUtilisateurDashboard($name)
    {
        $select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
        $select->setIntegrityCheck(false)
                ->join('utilisateur_dashboard',
                        'dashboard.IdDashboard = utilisateur_dashboard.IdDashboard',
                        array('utilisateur_dashboard.IdUtilisateur'))
                ->where('dashboard.Url = ?', $name);
        $rowset = $this->fetchAll($select);
        $count = 0;
        foreach ($rowset as $row)
        {
            $count++;
        }
        return $count;
    }

    public function retirerModule($name)
    {
        if ($this->getNbModuleUtilisateurDashboard($name) == 0)
        {
            $this->delete(array("Url = ?" => $name));
            if ($this->getNbModule($name) == 0)
            {
                return true;
            }
        }
        return false;
    }

}
?>
